home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / asyncpb / asyncpb.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.4 KB  |  483 lines

  1. /*
  2.     File:        AsyncPB.c
  3.     
  4.     Description:AsyncPB is an example of how File System calls can be made 
  5.                 in a chain from an interrupt handler like the Time Manager.
  6.                 Once you select DoIt from the menu give it a little time to actually
  7.                 process the input file.
  8.  
  9.     Author:        JB
  10.  
  11.     Copyright:     Copyright: © 1984-1999 by Apple Computer, Inc.
  12.                 all rights reserved.
  13.     
  14.     Disclaimer:    You may incorporate this sample code into your applications without
  15.                 restriction, though the sample code has been provided "AS IS" and the
  16.                 responsibility for its operation is 100% yours.  However, what you are
  17.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  18.                 after having made changes. If you're going to re-distribute the source,
  19.                 we require that you make it clear in the source that the code was
  20.                 descended from Apple Sample Code, but that you've made changes.
  21.     
  22.     Change History (most recent first):
  23.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  24.  
  25. */
  26.  
  27. #include    <Aliases.h>
  28. #include    <AppleEvents.h>
  29. #include    <Balloons.h>
  30. #include    <Controls.h>
  31. #include    <Desk.h>
  32. #include    <Dialogs.h>
  33. #include    <Events.h>
  34. #include    <Errors.h>
  35. #include    <Files.h>
  36. #include    <Folders.h>
  37. #include    <Fonts.h>
  38. #include    <GestaltEqu.h>
  39. #include    <Icons.h>
  40. #include    <Memory.h>
  41. #include    <Menus.h>
  42. #include     <OSUtils.h>
  43. #include    <Packages.h>
  44. #include    <Printing.h>
  45. #include    <Quickdraw.h>
  46. #include    <Resources.h>
  47. #include    <Script.h>
  48. #include    <StandardFile.h>
  49. #include    <String.h>
  50. #include    <Strings.h>
  51. #include     <LowMem.h>
  52. #include    <TextEdit.h>
  53. #include    <Timer.h>
  54. #include     <ToolUtils.h>
  55. #include    <Traps.h>
  56. #include    <Types.h>
  57. #include    <Windows.h>
  58. #include     "AsyncPB.h"
  59.  
  60. Boolean                gInForeGround            = true;
  61. Boolean                gProcessing                = true;
  62. unsigned long        gSleep                    = 0;
  63.  
  64. #ifdef powerc
  65. QDGlobals qd;
  66. #endif
  67.  
  68. #ifndef powerc
  69. MyParamBlockPtr        GetParmBlockPtr()        = { 0x2008 };     // move.l a0, d0 ;move the pointer to where MPW C places function results
  70. MyTaskPtr            GetTMTaskPtr()            = { 0x2009 };    // move.l a1, d0 ;move the pointer to where MPW C places function results
  71. #endif
  72.  
  73. #ifdef powerc
  74. pascal void PBWriteIOCompletion(ParmBlkPtr parmblkptr)
  75. #else
  76. pascal void PBWriteIOCompletion()
  77. #endif
  78.  
  79. {
  80.     MyParamBlockPtr        pb;
  81.     
  82.     #ifdef powerc
  83.     pb = (MyParamBlockPtr)parmblkptr;
  84.     #else
  85.     pb = GetParmBlockPtr();
  86.     #endif
  87.     
  88.     if (noErr == (*pb).ioParam.ioResult)
  89.     {
  90.         PrimeTime((QElemPtr) (*pb).tm, 1000 * 1);
  91.     }
  92. }
  93.  
  94.  
  95. #ifdef powerc
  96. pascal void PBReadIOCompletion(ParmBlkPtr parmblkptr)
  97. #else
  98. pascal void PBReadIOCompletion()
  99. #endif
  100. {    
  101.  
  102.     MyParamBlockPtr        pb;
  103.  
  104.     #ifdef powerc
  105.     pb = (MyParamBlockPtr)parmblkptr;
  106.     #else
  107.     pb = GetParmBlockPtr();
  108.     #endif
  109.     
  110.     if (noErr == (*pb).ioParam.ioResult)
  111.     {
  112.         (*pb).ioParam.ioCompletion    = (*pb).writeIOCompletion;
  113.         (*pb).ioParam.ioRefNum        = (*pb).writeFileRefNum;
  114.         (*pb).ioParam.ioReqCount    = (*pb).ioParam.ioActCount;
  115.         (*pb).ioParam.ioPosMode        = fsAtMark;
  116.         (*pb).ioParam.ioPosOffset    = 0;
  117.         PBWriteAsync((ParmBlkPtr) pb);
  118.     }
  119. }
  120.  
  121.  
  122.  
  123. #ifdef powerc
  124. pascal void PBSetFPosIOCompletion(ParmBlkPtr parmblkptr)
  125. #else
  126. pascal void PBSetFPosIOCompletion()
  127. #endif
  128.  
  129. {   
  130.   
  131.     MyParamBlockPtr        pb;
  132.     
  133.     #ifdef powerc
  134.     pb = (MyParamBlockPtr)parmblkptr;
  135.     #else
  136.     pb = GetParmBlockPtr();
  137.     #endif
  138.  
  139.     if (noErr == (*pb).ioParam.ioResult)
  140.     {
  141.         (*pb).ioParam.ioCompletion    = (*pb).readIOCompletion;
  142.         (*pb).ioParam.ioRefNum        = (*pb).readFileRefNum;
  143.         (*pb).ioParam.ioReqCount    = 1;
  144.         (*pb).ioParam.ioPosMode        = fsAtMark;
  145.         (*pb).ioParam.ioPosOffset    = 0;
  146.         PBReadAsync((ParmBlkPtr) pb);
  147.     }
  148. }
  149.  
  150. #ifdef powerc
  151. pascal void TMTaskProc(MyTaskPtr tm)
  152. #else
  153. pascal void TMTaskProc()
  154. #endif
  155. {
  156.   
  157.     /* on the PowerPC, the task record ptr is passed as a parameter */
  158.     /* in 68K we have to get it from A1 */
  159.     
  160.     #ifndef powerc
  161.         MyTaskPtr    tm;
  162.        tm = GetTMTaskPtr(); 
  163.     #endif
  164.     
  165.     (*(*tm).pb).ioParam.ioCompletion    = (*(*tm).pb).setFPosIOCompletion;
  166.     (*(*tm).pb).ioParam.ioRefNum        = (*(*tm).pb).readFileRefNum;
  167.     (*(*tm).pb).ioParam.ioPosMode        = fsFromMark;
  168.     (*(*tm).pb).ioParam.ioPosOffset        = 2;
  169.     PBSetFPosAsync((ParmBlkPtr) (*tm).pb);
  170. }
  171.  
  172. void DoMenu(short menu, short item)
  173. {
  174.     OSErr                error        = noErr;
  175.     Str255                daName;
  176.     short                daRefNum;
  177.     Point                where        = { 30, 10 };
  178.  
  179.     switch (menu)
  180.     {
  181.         case AppleMenu :
  182.         {
  183.             switch (item)
  184.             {
  185.                 case AboutItem :
  186.                 {
  187.                     Alert(AboutAlert, nil);
  188.                 }
  189.                 break;
  190.                 
  191.                 default :
  192.                 {
  193.                     GetMenuItemText(GetMenuHandle(AppleMenu), item, daName);
  194.                     daRefNum = OpenDeskAcc(daName); 
  195.                 }
  196.                 break;
  197.             }
  198.         }
  199.         break;
  200.         
  201.         case FileMenu :
  202.         {            
  203.             switch (item)
  204.             {
  205.                 case DoIt :
  206.                 {
  207.                     DisableItem(GetMenuHandle(FileMenu), DoIt);
  208.  
  209.                     error = FSOpen("\pinData", 0, &gPB.readFileRefNum);    
  210.                     
  211.                     error = FSDelete("\pOutData", 0);
  212.                     error = Create("\pOutData", 0, 'CWIE', 'TEXT');
  213.                     error = FSOpen("\pOutData", 0, &gPB.writeFileRefNum);
  214.                     
  215.                     gPB.ioParam.ioBuffer        = &gBuffer;
  216.                     gPB.tm                        = (TMTaskPtr) &gTM;
  217.  
  218.                     gPB.setFPosIOCompletion        = NewIOCompletionProc (PBSetFPosIOCompletion);        
  219.                     gPB.readIOCompletion        = NewIOCompletionProc (PBReadIOCompletion);        
  220.                     gPB.writeIOCompletion        = NewIOCompletionProc (PBWriteIOCompletion);        
  221.                     
  222.                 #if false
  223.  
  224.                     gPB.ioParam.ioCompletion    = gPB.setFPosIOCompletion;
  225.                     gPB.ioParam.ioRefNum        = gPB.readFileRefNum;
  226.                     gPB.ioParam.ioPosMode        = fsFromMark;
  227.                     gPB.ioParam.ioPosOffset        = 3;
  228.                     PBSetFPosAsync((ParmBlkPtr) &gPB);
  229.                 #else
  230.                     gTM.theTMTask.qType    = 0;
  231.                     gTM.theTMTask.tmCount    = 0;
  232.  
  233.                     gTM.theTMTask.tmAddr    = NewTimerProc(TMTaskProc);
  234.                     gTM.pb        = &gPB;
  235.  
  236.                     InsTime((QElemPtr) &gTM);
  237.                     
  238.                     PrimeTime((QElemPtr) &gTM, 30 * 1);
  239.                 #endif
  240.                 
  241.                 }
  242.                 break;
  243.                                 
  244.                 case QuitItem :
  245.                 {
  246.                     RmvTime((QElemPtr)&gTM);
  247.                     gProcessing = false;
  248.                 }
  249.                 break;
  250.             }
  251.         }
  252.         break;
  253.         
  254.         default :
  255.         {
  256.         }
  257.         break;
  258.         
  259.     }
  260. }
  261.         
  262. void EventLoop()
  263. {
  264.     OSErr                error         = noErr;
  265.     EventRecord            event;
  266.     Boolean                gotEvent;
  267.     WindowPtr            window;
  268.     DialogPtr            dialog;
  269.     short                itemHit;
  270.     short                part;
  271.     long                menuItem;
  272.     short                menu;
  273.     short                item;
  274.  
  275.     gotEvent = WaitNextEvent(everyEvent, &event, gSleep, nil);
  276.     
  277.     if (IsDialogEvent(&event))
  278.     {
  279.         if (! ((keyDown == event.what || autoKey == event.what) && (event.modifiers & cmdKey)))
  280.         {
  281.             if (DialogSelect(&event, &dialog, &itemHit))
  282.             {
  283.             }
  284.         }
  285.     }
  286.     else
  287.     {
  288.         switch (event.what)
  289.         {
  290.             case nullEvent:
  291.             {
  292.             }
  293.             break;
  294.                         
  295.             case mouseDown:
  296.             {
  297.                 switch (part = FindWindow(event.where, &window))
  298.                 {
  299.                     case inMenuBar:
  300.                     {
  301.                         menuItem = MenuSelect(event.where);
  302.                         menu = HiWord(menuItem);
  303.                         item = LoWord(menuItem);
  304.                         DoMenu(menu, item);
  305.                         HiliteMenu(0);
  306.                     }
  307.                     break;
  308.     
  309.                     case inContent :
  310.                     {
  311.                         if (! (*(WindowPeek)window).hilited)
  312.                         {
  313.                             SelectWindow(window);
  314.                         }
  315.                         else
  316.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  317.                         {
  318.                         }
  319.                     }
  320.                     break;
  321.                     
  322.                     case inGrow :
  323.                     {
  324.                         #define        kMinWidth        128
  325.                         #define        kMinWeight        64
  326.                         auto        Rect            limitRect;
  327.                         auto        long            newSize;
  328.                         
  329.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  330.                         {
  331.                             limitRect = qd.screenBits.bounds;
  332.                             InsetRect(&limitRect, 8, 8);
  333.                             limitRect.top    = kMinWeight;
  334.                             limitRect.left    = kMinWidth;
  335.                             newSize = GrowWindow(window, event.where, &limitRect);
  336.                             if (0 != newSize)
  337.                             {
  338.                                 SizeWindow(window, LoWord(newSize), HiWord(newSize), true);
  339.                             }
  340.                         }
  341.                     }
  342.                     break;
  343.                     
  344.                     case inDrag:
  345.                     {
  346.                         DragWindow(window, event.where, &qd.screenBits.bounds);
  347.                     }
  348.                     break;
  349.                     
  350.                     case inGoAway :
  351.                     {
  352.                         if (TrackGoAway(window, event.where))
  353.                         {
  354.                         }
  355.                     }
  356.                     break;
  357.                 
  358.                     case inZoomIn :
  359.                     case inZoomOut :
  360.                     {
  361.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  362.                         {
  363.                             if ( TrackBox(window, event.where, part) )
  364.                             {
  365.                                 SetPort(window);
  366.                                 EraseRect(&window->portRect);
  367.                                 ZoomWindow(window, part, true);
  368.                                 InvalRect(&window->portRect);
  369.                             }
  370.                         }
  371.                     }
  372.                     break;
  373.                     
  374.                 }
  375.             }
  376.             break;
  377.             
  378.             case updateEvt:
  379.             {
  380.                 window = (WindowPtr) event.message;
  381.                 if (kWindowKind == (*(WindowPeek)window).windowKind)
  382.                 {
  383.                     SetPort(window);
  384.                     BeginUpdate(window);
  385.                     {
  386.                     }
  387.                     EndUpdate(window);
  388.                 }
  389.             }
  390.             break;
  391.             
  392.             case activateEvt:
  393.             {
  394.                 window = (WindowPtr) event.message;
  395.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  396.                 {
  397.                 }
  398.             }
  399.             break;
  400.             
  401.             case autoKey:        
  402.             case keyDown:
  403.             {
  404.                 auto    char    key;
  405.                 
  406.                 key = event.message & charCodeMask;
  407.                 if (event.modifiers & cmdKey)
  408.                 {
  409.                     menuItem = MenuKey(key);
  410.                     menu = HiWord(menuItem);
  411.                     item = LoWord(menuItem);
  412.                     DoMenu(menu, item);
  413.                 }
  414.                 else
  415.                 {
  416.                     window = FrontWindow();
  417.                     if (kWindowKind == (*(WindowPeek)window).windowKind)
  418.                     {
  419.                     }
  420.                 }
  421.             }
  422.             break;
  423.             
  424.             case osEvt:
  425.             {
  426.                 switch ((event.message >> 24) & 0x0FF)
  427.                 {
  428.                     case suspendResumeMessage:
  429.                     {
  430.                         gInForeGround = 0 != (event.message & resumeFlag);
  431.                     }
  432.                     break;
  433.                 }
  434.             }
  435.             break;
  436.             
  437.             case kHighLevelEvent:
  438.             {
  439.                 error = AEProcessAppleEvent(&event);
  440.             }
  441.             break;
  442.         }
  443.     }
  444. }
  445.  
  446. main ()
  447. {            
  448.     OSErr                error         = noErr;
  449.     Handle                menuBar;
  450.     
  451.     InitGraf(&qd.thePort);
  452.     InitFonts();
  453.     InitWindows();
  454.     InitMenus();
  455.     TEInit();
  456.     InitDialogs(nil);
  457.     InitCursor();
  458.  
  459.  
  460.     menuBar = GetNewMBar(ApplicationMenuBar);
  461.     if (nil == menuBar)
  462.     {
  463.         error = ResError();
  464.         if (noErr == error)
  465.         {
  466.             error = resNotFound;
  467.             
  468.             ExitToShell();
  469.         }
  470.     }
  471.     else
  472.     {
  473.         SetMenuBar(menuBar);
  474.         DisposeHandle(menuBar);
  475.         AppendResMenu(GetMenuHandle(AppleMenu), 'DRVR');
  476.         DrawMenuBar();
  477.     }
  478.     
  479.         
  480.     while (gProcessing)
  481.         EventLoop();
  482.     return 0;
  483. }